package com.lagou.dao.impl;


import javax.sql.rowset.CachedRowSet;

import com.lagou.dao.BusinessDao;

import com.lagou.entity.Business;

import com.lagou.util.JDBCUtil2;

public class BusinessDaoImpl implements BusinessDao{
	JDBCUtil2 util=JDBCUtil2.getInit();
	@Override
	public Business login(String email, String pwd) {
		
		Business b=null;
			
			
			CachedRowSet rs=util.query("select * from t_business where b_email=? and b_pwd=?", email,pwd);
			try{
				if(rs.next()){
					b=new Business();
					b.setBid(rs.getInt("b_id"));
					b.setBemail(rs.getString("b_email"));
					b.setBpwd(rs.getString("b_pwd"));
					b.setBstatus(rs.getInt("b_status"));
					
				}
			}catch(Exception e){
				e.printStackTrace();
			}finally{
				util.closeCacheRowSet(rs);
			}
			return b;
	}
	@Override
	public boolean add(Business u) {
		boolean flag=util.update("insert into t_business (b_email,b_pwd) values(?,?)", u.getBemail(),u.getBpwd());
		if(flag) {
			return true;
		}else {
			return false;
		}
	}
	@Override
	public boolean queryByEmail(Business u) {
		String sql="SELECT *  from t_business where b_email=?";
		CachedRowSet rs=util.query(sql,u.getBemail());
		boolean flag=false;
		try {
			if(rs.next()) {
				
				flag=true;
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally {
			util.closeCacheRowSet(rs);
		}
		return flag;
	}
	@Override
	public boolean updateByEmail(Business u) {
		boolean flag=util.update("UPDATE t_business SET b_pwd =? WHERE b_email=? ", u.getBpwd(),u.getBemail());
		if(flag) {
			return true;
		}else {
			return false;
		}
	}
	@Override
	public boolean updatebpwdByemail(String email, String pwd) {
		return util.update("UPDATE t_business SET b_pwd =? WHERE b_email=? ", pwd,email);
	}
	
}
